import 'dart:convert';
// ------------------------------------------------------------------------------------- //

class ApiResModel {
  final bool success;
  final String code;
  final String message;
  final Map<String, dynamic> data;

  // Factory constructor to create an instance from a JSON string
  factory ApiResModel.fromStr(String jsonStr) {
    final Map<String, dynamic> jsonMap = json.decode(jsonStr);

    /// debug:
    // print('jsonMap: ${jsonMap}');
    bool parseSuccess(dynamic value) {
      if (value is bool) return value;
      if (value is String) return value.toLowerCase() == 'true';
      if (value is num) return value != 0;
      return false;
    }

    // print('1--${jsonMap['data']['data']}');
    return ApiResModel._(
      success: parseSuccess(jsonMap['success']),
      code: jsonMap['code']?.toString() ?? '',
      message: jsonMap['message'] ?? '',
      data: jsonMap['data'] ?? {},
    );
  }

  // Private constructor to initialize properties
  const ApiResModel._({
    required this.success,
    required this.code,
    required this.message,
    required this.data,
  });

  // Method to convert the object back to JSON
  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'code': code,
      'message': message,
      'data': data,
    };
  }

  @override
  String toString() {
    return '{'
        'success: $success,'
        'code: $code,'
        'message: $message,'
        'data: $data'
        '}';
  }
}
